﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Tetrinet;

namespace Tetrinet
{
    

    public partial class MainPage : UserControl
    {
        const int FIELDWIDTH = 12;
        const int FIELDHEIGHT = 22;
        int[,] field1 = new int[FIELDWIDTH, FIELDHEIGHT];

        public MainPage()
        {
            InitializeComponent();
            Tetriphone.netcode.Instance.MessageCallback = MessageCallback;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Tetriphone.netcode.Instance.ConnectToServer("192.168.0.185", 31457);
            //Tetriphone.netcode.Instance.ConnectToServer("192.168.1.7", 31457);
            Tetriphone.netcode.Instance.ConnectToServer("192.168.0.239", 31457);
        }

        private void MessageCallback(string message)
        {
            if (message.StartsWith("f"))
            {
                ProcessField(message);
            }
   
            Deployment.Current.Dispatcher.BeginInvoke(() => {

                TextBlock b = new TextBlock() { Text = message, Margin = new Thickness(1,1,1,1), Padding = new Thickness(1,1,1,1) };
                listBox1.Items.Add(b);
                listBox1.UpdateLayout();
                listBox1.ScrollIntoView(b);    
            });
        }

        void ProcessField(string message)
        {
            //string[] fieldData = message.Split(' ');
            //if (fieldData.Length != 3)
            //{
            //    return;
            //}
            //if (fieldData[1] == "1")
            //{
            //    // its our field
            //    if ((int)fieldData[2][0] >= (int)'0')
            //    {
            //        // they have sent the whole field
            //        for (int y = 0; y < FIELDHEIGHT; y++)
            //            for (int x = 0; x < FIELDWIDTH; x++)
            //                field1[x, y] = (int)fieldData[2][y * FIELDWIDTH + x] - (int)'0';

            //    }
            //    else
            //    {
            //        // they've just sent an update
            //        int j, x, y, block = 0;

            //        for (j = 0; j < FIELDWIDTH * FIELDHEIGHT && j < fieldData[2].Length; j++)
            //        {
            //            /* block information? */
            //            if ((int)fieldData[2][j] >= (int)'!' && (int)fieldData[2][j] < (int)'0')
            //                block = (int)fieldData[2][j] - (int)'!';
            //            else
            //            /* position */
            //            {
            //                x = (int)fieldData[2][j] - (int)'3'; j++;
            //                y = (int)fieldData[2][j] - (int)'3';
            //                if (x >= 0 && x < FIELDWIDTH && y >= 0 && y < FIELDHEIGHT)
            //                    field1[x, y] = block;
            //            }
            //        }
            //    }
            //    Deployment.Current.Dispatcher.BeginInvoke(() =>
            //    {
            //        silverlightControl3.UpdateField(field1);

            //    });
            //}
        }
   }
}
